using System;
using System.Collections.Generic;

[Serializable]
public class CollectorAchievement : GameplayAchievement
{
	[Serializable]
	public enum CollectionType
	{
		Stars = 0,
		Coins = 1,
		Tokens = 2,
		GGDollars = 3,
		Medals = 4,
		SentEnergy = 5
	}

	[Serializable]
	public class Stage
	{
		public int stageValue;

		public string reward;
	}

	public const AchievementResource.AchievementType achievementType = AchievementResource.AchievementType.CollectorAchievement;

	public CollectionType collectionType;

	public bool ignoreMatchType;

	public List<MatchType> eligibleMatchTypes = new List<MatchType>();

	public List<Stage> stages = new List<Stage>();

	public override bool shouldShowAchievementStageCompleted
	{
		get
		{
			return model.stageReported < achievedStagesCount;
		}
	}

	public override int achievedStagesCount
	{
		get
		{
			for (int i = 0; i < stages.Count; i++)
			{
				if (model.scoreAchieved < stages[i].stageValue)
				{
					return i;
				}
			}
			return stages.Count;
		}
	}

	public override int totalStages
	{
		get
		{
			return stages.Count;
		}
	}

	public override void OnShowAchievementStageCompleted(int achievedStageCount)
	{
		model.stageReported = achievedStageCount;
		ApplyRewards();
		Save();
	}

	public override void UpdateAchievement(GGStoryAchivementsController.UpdateAchievementParams updateParams)
	{
		GGStoryAchivementsController.UpdateAchievementResourceCollected updateAchievementResourceCollected = updateParams as GGStoryAchivementsController.UpdateAchievementResourceCollected;
		if (updateAchievementResourceCollected != null)
		{
			ReportMatchCompleted(updateAchievementResourceCollected);
			if (shouldShowAchievementStageCompleted)
			{
				achivementController.AchievementAccomplishedNotify(this);
			}
		}
	}

	public void ReportMatchCompleted(GGStoryAchivementsController.UpdateAchievementResourceCollected resourceCollectedUpdate)
	{
		if (collectionType == collectionType && (ignoreMatchType || IsMatchEligible(resourceCollectedUpdate.matchParams.matchType)))
		{
			model.scoreAchieved += resourceCollectedUpdate.scoreEarned;
			Save();
		}
	}

	private bool IsMatchEligible(MatchType matchType)
	{
		foreach (MatchType eligibleMatchType in eligibleMatchTypes)
		{
			if (eligibleMatchType == matchType)
			{
				return true;
			}
		}
		return false;
	}

	public override void ApplyRewards()
	{
	}

	public override bool HasCompletedStage(int stageIndex)
	{
		if (stageIndex >= stages.Count)
		{
			return false;
		}
		return stageIndex < achievedStagesCount;
	}

	public override string GetStageText(int stageIndex)
	{
		if (stageIndex >= stages.Count)
		{
			return string.Empty;
		}
		return stages[stageIndex].stageValue.ToString();
	}
}
